package com.zgw.his.design.resource;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.zgw.his.common.widgets.lktable.KTableCellSelectionListener;
import com.zgw.his.common.widgets.lktable.LKCellEditBase;
import com.zgw.his.common.widgets.lktable.LKTable;
import com.zgw.his.common.widgets.lktable.LKTableUtil.ECellEditType;
import com.zgw.his.common.widgets.lktable.SWTX;
import com.zgw.his.design.common.RcpResourceManager;
import com.zgw.his.server.emr.IEmrService;
import com.zgw.his.server.emr.entity.TpatientDataItem;
import com.zgw.his.server.exceptions.BusinessException;
import com.zgw.his.server.util.SpringBeanFactory;

/**
 * 数据源项目设置
 * 
 * @see
 * */
public class ItemSetDlg extends Dialog {
	private static final IEmrService service = SpringBeanFactory
			.getServiceBean(IEmrService.class);

	protected Object result;
	protected Shell shell;
	private Composite composite;
	private Text text;
	private LKTable lktable;
	private ToolItem toolItem;
	private ToolItem toolItem_1;
	private ToolItem toolItem_2;
	private Button btnc;
	private List<TpatientDataItem> items;
	private List<TpatientDataItem> updateItems = new ArrayList<TpatientDataItem>();

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public ItemSetDlg(Shell parent, int style) {
		super(parent, style);
		setText("数据项设置");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	public static void main(String[] args) {
		ItemSetDlg d = new ItemSetDlg(new Shell(), SWT.NONE);
		d.open();
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX
				| SWT.APPLICATION_MODAL);
		shell.setSize(580, 480);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		{
			composite = new Composite(shell, SWT.NONE);
			{
				FormData formData = new FormData();
				formData.top = new FormAttachment(0);
				formData.left = new FormAttachment(0);
				formData.bottom = new FormAttachment(100, -35);
				formData.right = new FormAttachment(100);
				composite.setLayoutData(formData);
			}
			composite.setLayout(new GridLayout(2, false));
			{
				text = new Text(composite, SWT.BORDER);
				text.setMessage("在些输入名称或编号进行查找");
				{
					GridData gridData = new GridData(SWT.FILL, SWT.CENTER,
							true, false, 1, 1);
					gridData.heightHint = 18;
					text.setLayoutData(gridData);
				}
			}
			new Label(composite, SWT.NONE);
			{

				lktable = new LKTable(composite, SWT.V_SCROLL | SWT.H_SCROLL
						| SWTX.WRAP | SWTX.EDIT_ON_KEY);
				lktable.setBackground(RcpResourceManager
						.getColor(SWT.COLOR_WHITE));
				lktable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
						true, 1, 1));
			}
			{
				ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT
						| SWT.VERTICAL);
				toolBar.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false,
						false, 1, 1));
				{
					toolItem = new ToolItem(toolBar, SWT.NONE);
					toolItem.setText("新增");
					toolItem.addSelectionListener(new SelectionAdapter() {
						@Override
						public void widgetSelected(SelectionEvent e) {
							doAdd();
						}
					});
				}
				{
					toolItem_2 = new ToolItem(toolBar, SWT.SEPARATOR);
				}
				{
					toolItem_1 = new ToolItem(toolBar, SWT.NONE);
					toolItem_1.setText("删除");
					toolItem_1.addSelectionListener(new SelectionAdapter() {
						@Override
						public void widgetSelected(SelectionEvent e) {
							doRemove();
						}
					});
				}
			}
		}
		{
			btnc = new Button(shell, SWT.NONE);
			{
				FormData formData = new FormData();
				formData.bottom = new FormAttachment(100, -10);
				formData.right = new FormAttachment(100, -43);
				formData.height = 20;
				btnc.setLayoutData(formData);
			}
			btnc.setText("关   闭&C");
			btnc.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					shell.close();
				}
			});
		}
		{
			Button btns = new Button(shell, SWT.NONE);
			{
				FormData formData = new FormData();
				formData.bottom = new FormAttachment(btnc, 0, SWT.BOTTOM);
				formData.right = new FormAttachment(btnc, -6);
				formData.height = 20;
				btns.setLayoutData(formData);
			}
			btns.setText("确   定&S");
			btns.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					doOK();
				}
			});
		}
		fillTable();
		initialize();
	}

	private TpatientDataItem dataItem;

	// 构建表格
	private void fillTable() {
		lktable.doAddColumn(0, "名称", "fname", 70, SWTX.ALIGN_HORIZONTAL_LEFT,
				ECellEditType.textBox, true);
		lktable.doAddColumn(1, "编号", "fcode", 200, SWTX.ALIGN_HORIZONTAL_LEFT,
				ECellEditType.textBox, true);
		lktable.doAddColumn(2, "备注", "fremark", 70, SWTX.ALIGN_HORIZONTAL_LEFT,
				ECellEditType.textBox, true);

		LKCellEditBase cellEditBase = new LKCellEditBase() {
			@Override
			public void doAfterOpen(LKTable lkTable, Object obj, String feild,
					Control m_control) {
				if (!updateItems.contains(obj)
						&& obj instanceof TpatientDataItem) {
					updateItems.add((TpatientDataItem) obj);
				}
			}
		};
		lktable.addCellEditBase("fname", cellEditBase);
	}

	private void initialize() {
		try {
			items = service.getAllEntity(TpatientDataItem.class);
			setInput(items);
		} catch (BusinessException e) {
			e.printStackTrace();
		}

		lktable.addCellSelectionListener(new KTableCellSelectionListener() {
			public void fixedCellSelected(int col, int row, int statemask) {

			}

			public void cellSelected(int col, int row, int statemask) {
				result = lktable.getSelection();
			}
		});

	}

	public void setInput(List<TpatientDataItem> items) {
		lktable.setInput(items);
	}

	private void doOK() {
		try {
			service.saveOrUpdateAll(updateItems);
			updateItems.clear();
		} catch (BusinessException e) {
			e.printStackTrace();
			MessageDialog.openError(getParent(), "", "保存失败" + e.getMessage());
		}
		shell.close();
	}

	private void doAdd() {
		try {
			int code = findMaxCode() + 1;
			TpatientDataItem item = new TpatientDataItem();
			item.setFcode(code + "");
			items.add(item);
			lktable.editElement(item, "fname");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doRemove() {
		Object obj = lktable.getSelection();
		if (obj instanceof TpatientDataItem) {
			MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK
					| SWT.CANCEL);
			box.setMessage("确定删除?");
			if (box.open() == SWT.OK) {
				try {
					if (((TpatientDataItem) obj).getFid() != null)
						service.delete(obj);
				} catch (BusinessException e) {
					e.printStackTrace();
				}
				lktable.remove(obj);
				if (items.contains(obj)) {
					items.remove(obj);
				}
			}
		}
	}

	public int findMaxCode() throws BusinessException {
		List list = service.find("select max(fcode) from TpatientDataItem");
		if (list != null && list.size() > 0 && list.get(0) != null) {
			return (Integer) list.get(0);
		}
		return 0;
	}
}
